package viqiang.ye.pattern.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler implements InvocationHandler {

    Subject subject ;

    public MyInvocationHandler(Subject subject) {
        this.subject = subject;
    }

    /**
     *
     * @param proxy 代理类
     * @param method 正在调用的方法
     * @param args 参数
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("sellBooks".equals(method.getName())) {
            System.out.println("调用的是卖书的方法");
            return method.invoke(subject, args);
        } else if("speak".equals(method.getName())) {
            System.out.println("调用的是说话的方法");
            return method.invoke(subject, args);
        } else {
            return null;
        }
    }
}
